package com.tyj.movie.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tyj.movie.entity.*;
import com.tyj.movie.mapper.MovieAreaRelationMapper;
import com.tyj.movie.mapper.MovieTypeRelationMapper;
import com.tyj.movie.service.*;
import com.tyj.movie.to.MovieTo;
import com.tyj.movie.mapper.MovieMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tyj
 * @since 2021-06-28
 */
@Service
@Slf4j
public class MovieServerImpl extends ServiceImpl<MovieMapper, Movie> implements MovieServer {

    @Autowired
    MovieMapper movieMapper;

    @Autowired
    ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    MoviePictureSetService moviePictureSetService;

    @Autowired
    MovieActorRelationService movieActorRelationService;

    @Autowired
    MovieTypeRelationService movieTypeRelationService;

    @Autowired
    MovieAreaRelationService movieAreaRelationService;

    @Autowired
    MovieTypeRelationMapper movieTypeRelationMapper;

    @Autowired
    MovieAreaRelationMapper movieAreaRelationMapper;

    @Override
    @Transactional
    public boolean addMovie(MovieTo movieTo) throws ExecutionException, InterruptedException {
        //1.封装movie对象
        Movie movie = new Movie();
        BeanUtils.copyProperties(movieTo, movie);
        movie.setGrade(0.0);
        movie.setWatchNumber(0L);
        movie.setState(0);
        movie.setCreateDate(new Date());


        //2.添加movie数据获取id
            movieMapper.insert(movie);
            log.debug("电影添加完成");

        //3.添加图片集
            String movieAtlas = movieTo.getMoviePictureSet();
            String[] pictureSets = movieAtlas.split(",");
            List<MoviePictureSet> list = new ArrayList<>();
            for (int i = 0; i < pictureSets.length; i++) {
                MoviePictureSet moviePictureSet = new MoviePictureSet();
                if (i == 0) { //是主图
                    moviePictureSet.setState(true);
                } else {   //否则都是附图
                    moviePictureSet.setState(false);
                }
                moviePictureSet.setMovieId(movie.getId());
                moviePictureSet.setPictureUrl(pictureSets[i]);
                list.add(moviePictureSet);
            }
            moviePictureSetService.saveBatch(list);
            log.debug("任务1成功");

        //4.添加影员关系表
            //添加导演 添加演员
            String directorsId = movieTo.getDirectorsIds();
            String[] directorsIds = directorsId.split(",");
            List<MovieActorRelation> lists = new ArrayList<>();
            for (String id : directorsIds) {
                MovieActorRelation movieActorRelation = new MovieActorRelation();
                movieActorRelation.setMovieActorId(Long.valueOf(id));
                movieActorRelation.setMovieId(movie.getId());
                lists.add(movieActorRelation);
            }

            String actorIds = movieTo.getActorIds();
            String[] actorIds1 = actorIds.split(",");
            for (String s : actorIds1) {
                MovieActorRelation movieActorRelation = new MovieActorRelation();
                String[] split = s.split(":");
                movieActorRelation.setMovieId(movie.getId());
                movieActorRelation.setMovieActorId(Long.valueOf(split[0]));
                movieActorRelation.setPortray(split[1]);
                lists.add(movieActorRelation);
            }
            movieActorRelationService.saveBatch(lists);
            log.debug("任务2成功");


        //5.添加类型
            List<Long> movieTypeIds = movieTo.getMovieTypeIds();
            List<MovieTypeRelation> list1 = new ArrayList<>();
            for (Long id : movieTypeIds) {
                MovieTypeRelation movieTypeRelation = new MovieTypeRelation();
                movieTypeRelation.setMovieId(movie.getId());
                movieTypeRelation.setMovieTypeId(id);
                list1.add(movieTypeRelation);
            }
            movieTypeRelationService.saveBatch(list1);
            log.debug("任务3成功");


        //5.添加区域
            List<Long> movieAreaIds = movieTo.getMovieAreaIds();
            List<MovieAreaRelation> list2 = new ArrayList<>();
            for (Long id : movieAreaIds) {
                MovieAreaRelation movieAreaRelation = new MovieAreaRelation();
                movieAreaRelation.setMovieId(movie.getId());
                movieAreaRelation.setMovieAreaId(id);
                list2.add(movieAreaRelation);
            }
            movieAreaRelationService.saveBatch(list2);
            log.debug("任务4成功");



        return true;
    }

    /**
     * 电影分页模糊查询
     *
     * @param pageNum   分页数
     * @param movieName 电影名
     * @return
     */
    @Override
    public IPage<Movie> moviePagingQuery(Long pageNum, String movieName) {
        IPage<Movie> page = movieMapper.moviePagingQuery(new Page<Movie>(pageNum, 5), movieName);
        List<Movie> records = page.getRecords();
        //封装类型数据
        records.stream().forEach((movie -> {
            //电影类型
            Long id = movie.getId();
            List<String> movieType = movieTypeRelationMapper.queryMovieTypeByMovieId(id);
            StringBuffer stringBuffer = new StringBuffer();
            for (String s : movieType) {
                stringBuffer.append(s + ",");
            }
            String substring = stringBuffer.substring(0, stringBuffer.length() - 1);
            movie.setType(substring);

            //电影区域
            List<String> movieArea=movieAreaRelationMapper.queryMovieAreaByMovieId(id);
            StringBuffer stringBuffer1 = new StringBuffer();
            for (String s : movieArea) {
                stringBuffer1.append(s + ",");
            }
            String substring1 = stringBuffer1.substring(0, stringBuffer1.length() - 1);
            movie.setArea(substring1);

        }));

        return page;
    }

    @Override
    public void enable(Long id) {
        movieMapper.recoveryById(id);
    }

    @Override
    public void disable(Long id) {
        movieMapper.deleteById(id);
    }
}
